package cate.game.play.config.skill;

import cate.common.table.fight.skill.row.SkillBaseRow;
import cate.common.table.fight.skill.row.SkillEnhanceRow;
import cate.common.util.XT;
import cate.game.play.fighter.Fighter;
import cate.game.play.skill.Skill;
import cate.game.play.skill.SkillFactory;
import cate.game.play.support.PlayBuildContext;
import cate.game.table.TableManager;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SkillCfgHolder {


    @NoteField("技能实例工厂")
    private SkillFactory factory = new SkillFactory();

    private Map<Integer, SkillCfg> map = new HashMap<>();

    public SkillCfg get(int id) {
        return map.get(id);
    }

    public void init(TableManager table) {
        map.clear();
        table.skill.base.getList().forEach(this::create);
    }

    public void init(PlayBuildContext context) {
        map.clear();
        context.skillExcel().base.getList().forEach(this::create);
    }

    private void create(SkillBaseRow row){
        SkillCfg cfg = new SkillCfg(row);
        map.put(cfg.id, cfg);
    }

    /**
     * 根据技能原始配置构建（用于需要临时篡改表中数据的技能）
     * @param owner
     * @return
     */
    public Skill build(Fighter owner, SkillCfg cfg) {
        return factory.build(owner, cfg);
    }

    /**
     * 根据id构建技能实例 需要根据英雄自身的技能进行强化
     * @param owner
     * @param skillId
     * @return
     */
    public Skill buildById(Fighter owner, int skillId) {
        SkillCfg cfg = this.get(skillId);
        if (cfg == null) {
            return null;
        }
        List<SkillEnhanceRow> enhanceList = owner.skill.getEnhance(cfg.row);
        if (XT.isNotBlank(enhanceList)) {
            cfg = new SkillCfg(cfg.row);
            for (SkillEnhanceRow enhanceRow : enhanceList) {
                cfg.enhance(enhanceRow);
            }
        }
        return build(owner, cfg);
    }
}
